package com.yycx.module.bus.provider.handler.invoice;

import com.yycx.common.base.module.export.ExportField;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 已开票税金明细列表扩展器
 *
 * @author zyf
 */
@Component("yesInvoiceDetailsPageListHandler")
public class YesInvoiceDetailsPageListHandler implements PageInterceptor<BusOrderDetails>, ExportInterceptor {

    @Autowired
    private BusPaymentService paymentService;

    @Override
    public void prepare(CriteriaQuery<BusOrderDetails> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        Long paymentId = params.getLong("paymentId");

        BusPayment payment=paymentService.getById(paymentId);
        Integer settleType = FlymeUtils.getInteger(payment.getSettleType(), 1);
        cq.addSelect(BusOrder.class, "orderId","orderNo", "sendDate","supplierId");
        cq.addSelect(BusOrderDetails.class,"orderDetailsId","salePrice", "saleNum","supplierRetable","supplierRetable2","returnDate","returnAmount");
        cq.addSelect(BusSupplier.class,"supplierName");
        cq.addSelect(BusCustom.class,"customName");
        cq.addSelect(BusDrugs.class, "drugsName", "drugsSpec");
        cq.eq(true, "bod.companyId", OpenHelper.getCompanyId());

        if(settleType.equals(1)) {
            cq.eq(true, "bod.kaipiaoId", paymentId);
        }else{
            //朗迪真奥医贸
            cq.eq(true, "bod.kaipiaoId2", paymentId);
        }
    }


    @Override
    public void initExcelExportEntity(ExportField exportField, List list) {
        Object key = exportField.getKey();
        exportField.setWidth(20);
    }
}
